package com.jushu.storbox.service;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.ErrorConstant;
import com.jushu.storbox.entities.DeviceInfo;
import com.jushu.storbox.manager.DeviceInfoManager;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.UserRequestV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.RefreshToken;
import com.huawei.sharedrive.sdk.android.modelV2.response.UserResponseV2;
import com.huawei.sharedrive.sdk.android.serviceV2.UserClientV2;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

/**
 * 
 * UserControl
 * 
 */
public class UserControl {

	private final static String TAG = "UserControl";
	
	/** FileHelper context */
	private Context context;
	
	static UserClientV2 userClient = ShareDriveApplication.getInstance().getUserClientV2();

	/**
	 * UserControl construct function
	 * 
	 * @param context
	 *            Context
	 */
	public UserControl(Context context) {
		super();
		this.context = context;
	}

	/**
	 * UserControl construct function
	 * 
	 */
	public UserControl() {
		super();
	}

	/**
	 * login
	 * 
	 * @param context
	 *            Context
	 * @param userName
	 *            String
	 * @param password
	 *            String
	 * @return UserResponse
	 * @throws ClientException
	 */
	public static UserResponseV2 Login(Context context, String userName,
			String password) throws ClientException {
		if(StringUtil.isBlank(userName) || StringUtil.isBlank(password)) {
			throw new ClientException(ErrorConstant.CLIENT_ERROR_LOGIN_INFO);
		}
		LogUtil.i(TAG, "Login...");
		DeviceInfoManager dim = new DeviceInfoManager(context);
		DeviceInfo deviceInfo = dim.getDeviceInfo();
		UserResponseV2 response = null;
		UserRequestV2 loginRequest = new UserRequestV2();
//		loginRequest.setDeviceType(deviceInfo.getDeviceType());
		loginRequest.setDeviceAgent(deviceInfo.getDeviceAgent());
		String[] account = userName.split("\\\\");
		if (account.length > 1){
			loginRequest.setDomain(account[0]);
			loginRequest.setLoginName(account[1]);
		} else {
			account = userName.split("/");
			if (account.length > 1){
				loginRequest.setDomain(account[0]);
				loginRequest.setLoginName(account[1]);
			} else {
				loginRequest.setLoginName(account[0]);
			}
		}

		loginRequest.setPassword(password);
		Log.i("test", "userName = " + loginRequest.getLoginName() + "     password = " + loginRequest.getPassword() + "      domain = " + loginRequest.getDomain());
		loginRequest.setDeviceAddress(deviceInfo.getDeviceAddress());
		loginRequest.setDeviceName(deviceInfo.getDeviceName());
		loginRequest.setDeviceOS(deviceInfo.getDeviceOS());
		loginRequest.setDeviceSN(deviceInfo.getDeviceSN());
		// loginRequest.setDeviceAddress("dsdgdssdafs465");
		// loginRequest.setDeviceName("samsung");
		// loginRequest.setDeviceOS("14654646767");
		// loginRequest.setDeviceSN("SN-safsaf1545454");
		response = userClient.loginV2(loginRequest);
		if(response == null || response.getCloudUserId() == null){
			return null;
		}
		return response;
	}
	
	public RefreshToken refreshToken(String refreshToken)
    {
        LogUtil.i(TAG, "refreshToken");
        RefreshToken newToken = null;
        try
        {
        	if(!refreshToken.equals(""))
        	{
        		newToken = userClient.refreshToken(refreshToken);
        	}
        }
        catch (ClientException e)
        {
            LogUtil.e(TAG, "e:" + e.getStatusCode());
            e.printStackTrace();
        }
        return newToken;
    }
}
